//
//  GHKeyBoardView.m
//  GHKeyBoard
//
//  Created by jansh on 16/1/11.
//  Copyright © 2016年 gh. All rights reserved.
//

#import "GHKeyBoardView.h"
#import "DeviceTools.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

// iPhone6_iPhone6s
#define kWidth 31
#define kHeight 45
#define kSpaceWidth (kScreenWidth - 10 * kWidth - 10) / 9
#define kNewSpaceWidth (kScreenWidth - 10 * kWidth - 10) / 8

// iPhone4_iPhone4s
#define iPhone4_iPhone4sWidth 28
#define iPhone4_iPhone4sHeight 34
#define iPhone4_iPhone4sSpaceWidth (kScreenWidth - 10 * iPhone4_iPhone4sWidth - 10) / 9
#define iPhone4_iPhone4sNewSpaceWidth (kScreenWidth - 10 * iPhone4_iPhone4sWidth - 10) / 8
#define iPhone4_iPhone4sSpaceHeight (self.bounds.size.height - 4 * iPhone4_iPhone4sHeight - 10)/ 3

// iPhone5_iPhone5s_iPhone5c
#define iPhone5_iPhone5s_iPhone5cWidth 28
#define iPhone5_iPhone5s_iPhone5cHeight 42
#define iPhone5_iPhone5s_iPhone5cSpaceWidth (kScreenWidth - 10 * iPhone5_iPhone5s_iPhone5cWidth - 10) / 9
#define iPhone5_iPhone5s_iPhone5cNewSpaceWidth (kScreenWidth - 10 * iPhone5_iPhone5s_iPhone5cWidth - 10) / 8
#define iPhone5_iPhone5s_iPhone5cSpaceHeight (self.bounds.size.height - 4 * iPhone5_iPhone5s_iPhone5cHeight - 10)/ 3

// iPhone6Plus_iPhone6sPlus
#define iPhone6Plus_iPhone6sPlusWidth 34
#define iPhone6Plus_iPhone6sPlusHeight 55
#define iPhone6Plus_iPhone6sPlusSpaceWidth (kScreenWidth - 10 * iPhone6Plus_iPhone6sPlusWidth - 10) / 9
#define iPhone6Plus_iPhone6sPlusNewSpaceWidth (kScreenWidth - 10 * iPhone6Plus_iPhone6sPlusWidth - 10) / 8
#define iPhone6Plus_iPhone6sPlusSpaceHeight (self.bounds.size.height - 4 * iPhone6Plus_iPhone6sPlusHeight - 10)/ 3


@interface GHKeyBoardView ()

@property (nonatomic, strong) NSArray *lowerCase;
@property (nonatomic, strong) NSArray *capitalArray;
@property (nonatomic, strong) UITextField *textF;

@end

@implementation GHKeyBoardView

- (instancetype)initWithTextField:(UITextField *)textF
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight*0.34);
        self.textF = textF;
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:@"NO" forKey:@"buttonState"];
        [user synchronize];
        [self addViews];
    }
    return self;
}

- (void)addViews
{
    self.lowerCase = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0",@"q",@"w",@"e",@"r",@"t",@"y",@"u",@"i",@"o",@"p",@"a",@"s",@"d",@"f",@"g",@"h",@"j",@"k",@"l",@"z",@"x",@"c",@"v",@"b",
                       @"n",@"m"];
    self.capitalArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0",@"Q",@"W",@"E",@"R",@"T",@"Y",@"U",@"I",@"O",@"P",@"A",@"S",@"D",@"F",@"G",@"H",@"J",@"K",@"L",@"Z",@"X",@"C",@"V",@"B",
                          @"N",@"M"];
    self.btnArray = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        if (i == 2) {
            for (int j = 0; j < 9; j ++) {
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                // 判断是那种机型
                if ([DeviceTools iPhone4_iPhone4s]) {
                    button.frame = CGRectMake(5 + iPhone4_iPhone4sWidth /2 + iPhone4_iPhone4sWidth * j + iPhone4_iPhone4sSpaceWidth*j, (iPhone4_iPhone4sHeight+iPhone4_iPhone4sSpaceHeight)*i+5, iPhone4_iPhone4sWidth, iPhone4_iPhone4sHeight);
                } else if ([DeviceTools iPhone5_iPhone5s_iPhone5c]) {
                    button.frame = CGRectMake(5 + iPhone5_iPhone5s_iPhone5cWidth / 2 + iPhone5_iPhone5s_iPhone5cWidth * j + iPhone5_iPhone5s_iPhone5cSpaceWidth*j, (iPhone5_iPhone5s_iPhone5cHeight+iPhone5_iPhone5s_iPhone5cSpaceHeight)*i+5, iPhone5_iPhone5s_iPhone5cWidth, iPhone5_iPhone5s_iPhone5cHeight);
                } else if ([DeviceTools iPhone6_iPhone6s]) {
                    button.frame = CGRectMake(5 + kWidth / 2 + kWidth * j + kSpaceWidth*j, (kHeight+10)*i+ 10, kWidth, kHeight);
                } else if ([DeviceTools iPhone6Plus_iPhone6sPlus]) {
                    button.frame = CGRectMake(5 + iPhone6Plus_iPhone6sPlusWidth / 2 + iPhone6Plus_iPhone6sPlusWidth * j + iPhone6Plus_iPhone6sPlusSpaceWidth*j, (iPhone6Plus_iPhone6sPlusHeight+iPhone6Plus_iPhone6sPlusSpaceHeight)*i+5, iPhone6Plus_iPhone6sPlusWidth, iPhone6Plus_iPhone6sPlusHeight);
                }
                [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
                button.titleLabel.font = [UIFont systemFontOfSize:24];
                button.titleLabel.textAlignment = NSTextAlignmentCenter;
                [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
                [button setBackgroundImage:[UIImage imageNamed:@"btnDselect"] forState:(UIControlStateNormal)];
                button.tag = [NSString stringWithFormat:@"%d%d", i,j].integerValue + 100;
                [self.btnArray addObject:button];
                [self addSubview:button];
            }
        } else {
            for (int j = 0; j < 10; j ++) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                // 判断是那种机型
                if ([DeviceTools iPhone4_iPhone4s]) {
                    btn.frame = CGRectMake(5 + iPhone4_iPhone4sWidth * j + iPhone4_iPhone4sSpaceWidth*j, (iPhone4_iPhone4sHeight+iPhone4_iPhone4sSpaceHeight)*i+5, iPhone4_iPhone4sWidth, iPhone4_iPhone4sHeight);
                } else if ([DeviceTools iPhone5_iPhone5s_iPhone5c]) {
                    btn.frame = CGRectMake(5 + iPhone5_iPhone5s_iPhone5cWidth * j + iPhone5_iPhone5s_iPhone5cSpaceWidth*j, (iPhone5_iPhone5s_iPhone5cHeight+iPhone5_iPhone5s_iPhone5cSpaceHeight)*i+5, iPhone5_iPhone5s_iPhone5cWidth, iPhone5_iPhone5s_iPhone5cHeight);
                } else if ([DeviceTools iPhone6_iPhone6s]) {
                    btn.frame = CGRectMake(5 + kWidth * j + kSpaceWidth*j, (kHeight+10)*i+ 10, kWidth, kHeight);
                } else if ([DeviceTools iPhone6Plus_iPhone6sPlus]) {
                    btn.frame = CGRectMake(5 + iPhone6Plus_iPhone6sPlusWidth * j + iPhone6Plus_iPhone6sPlusSpaceWidth*j, (iPhone6Plus_iPhone6sPlusHeight+iPhone6Plus_iPhone6sPlusSpaceHeight)*i+5, iPhone6Plus_iPhone6sPlusWidth, iPhone6Plus_iPhone6sPlusHeight);
                }
                [btn setBackgroundImage:[UIImage imageNamed:@"btnDselect"] forState:(UIControlStateNormal)];
                [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
                btn.tag = [NSString stringWithFormat:@"%d%d",i,j].integerValue+100;
                btn.titleLabel.font = [UIFont systemFontOfSize:24];
                btn.titleLabel.textAlignment = NSTextAlignmentCenter;
                [self.btnArray addObject:btn];
                [self addSubview:btn];
            }
        }
    }
    
    // 切换按钮
    self.altBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    if ([DeviceTools iPhone4_iPhone4s]) {
        self.altBtn.frame = CGRectMake(5, (iPhone4_iPhone4sHeight + iPhone4_iPhone4sSpaceHeight) * 3 + 5, iPhone4_iPhone4sWidth*1.5, iPhone4_iPhone4sHeight);
    } else if ([DeviceTools iPhone5_iPhone5s_iPhone5c]) {
        self.altBtn.frame = CGRectMake(5, (iPhone5_iPhone5s_iPhone5cHeight + iPhone5_iPhone5s_iPhone5cSpaceHeight) * 3 + 5, iPhone5_iPhone5s_iPhone5cWidth*1.5, iPhone5_iPhone5s_iPhone5cHeight);
    } else if ([DeviceTools iPhone6_iPhone6s]) {
        self.altBtn.frame = CGRectMake(5, (kHeight + 10) * 3 + 10, kWidth*1.5, kHeight);
    } else if ([DeviceTools iPhone6Plus_iPhone6sPlus]) {
        self.altBtn.frame = CGRectMake(5, (iPhone6Plus_iPhone6sPlusHeight + iPhone6Plus_iPhone6sPlusSpaceHeight) * 3 + 5, iPhone6Plus_iPhone6sPlusWidth*1.5, iPhone6Plus_iPhone6sPlusHeight);
    }
    
    [self.altBtn addTarget:self action:@selector(altBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.altBtn setBackgroundImage:[UIImage imageNamed:@"altBtn"] forState:(UIControlStateNormal)];
    [self addSubview:self.altBtn];
    
    // 删除按钮
    self.delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    if ([DeviceTools iPhone4_iPhone4s]) {
        self.delBtn.frame = CGRectMake(self.bounds.size.width-iPhone4_iPhone4sWidth * 1.5-5, (iPhone4_iPhone4sHeight + iPhone4_iPhone4sSpaceHeight) * 3 + 5, iPhone4_iPhone4sWidth*1.5, iPhone4_iPhone4sHeight);
    } else if ([DeviceTools iPhone5_iPhone5s_iPhone5c]) {
        self.delBtn.frame = CGRectMake(self.bounds.size.width-iPhone5_iPhone5s_iPhone5cWidth * 1.5-5, (iPhone5_iPhone5s_iPhone5cHeight + iPhone5_iPhone5s_iPhone5cSpaceHeight) * 3 + 5, iPhone5_iPhone5s_iPhone5cWidth*1.5, iPhone5_iPhone5s_iPhone5cHeight);
    } else if ([DeviceTools iPhone6_iPhone6s]) {
        self.delBtn.frame = CGRectMake(self.bounds.size.width-kWidth * 1.5-5, (kHeight + 10) * 3 + 10, kWidth*1.5, kHeight);
    } else if ([DeviceTools iPhone6Plus_iPhone6sPlus]) {
        self.delBtn.frame = CGRectMake(self.bounds.size.width-iPhone6Plus_iPhone6sPlusWidth * 1.5-5, (iPhone6Plus_iPhone6sPlusHeight + iPhone6Plus_iPhone6sPlusSpaceHeight) * 3 + 5, iPhone6Plus_iPhone6sPlusWidth*1.5, iPhone6Plus_iPhone6sPlusHeight);
    }
    [self.delBtn setBackgroundImage:[UIImage imageNamed:@"del"] forState:(UIControlStateNormal)];
    [self.delBtn setBackgroundImage:[UIImage imageNamed:@"delSelect"] forState:(UIControlStateHighlighted)];
    [self.delBtn addTarget:self action:@selector(delBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:self.delBtn];
    
    // 最后剩余的6个 Button
    int tags = 29;
    for (int i = 0; i < 7; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        if ([DeviceTools iPhone4_iPhone4s]) {
            btn.frame = CGRectMake(CGRectGetMaxX(self.altBtn.frame)+ iPhone4_iPhone4sNewSpaceWidth * i + iPhone4_iPhone4sWidth * i +iPhone4_iPhone4sNewSpaceWidth, (iPhone4_iPhone4sHeight + iPhone4_iPhone4sSpaceHeight) * 3 + 5, iPhone4_iPhone4sWidth, iPhone4_iPhone4sHeight);
        } else if ([DeviceTools iPhone5_iPhone5s_iPhone5c]) {
            btn.frame = CGRectMake(CGRectGetMaxX(self.altBtn.frame)+ iPhone5_iPhone5s_iPhone5cNewSpaceWidth * i + iPhone5_iPhone5s_iPhone5cWidth * i +iPhone5_iPhone5s_iPhone5cNewSpaceWidth, (iPhone5_iPhone5s_iPhone5cHeight + iPhone5_iPhone5s_iPhone5cSpaceHeight) * 3 + 5, iPhone5_iPhone5s_iPhone5cWidth, iPhone5_iPhone5s_iPhone5cHeight);
        } else if ([DeviceTools iPhone6_iPhone6s]) {
            btn.frame = CGRectMake(CGRectGetMaxX(self.altBtn.frame)+ kNewSpaceWidth * i + kWidth * i +kNewSpaceWidth, (kHeight + 10) * 3 + 10, kWidth, kHeight);
        } else if ([DeviceTools iPhone6Plus_iPhone6sPlus]) {
            btn.frame = CGRectMake(CGRectGetMaxX(self.altBtn.frame)+ iPhone6Plus_iPhone6sPlusNewSpaceWidth * i + iPhone6Plus_iPhone6sPlusWidth * i +iPhone6Plus_iPhone6sPlusNewSpaceWidth, (iPhone6Plus_iPhone6sPlusHeight + iPhone6Plus_iPhone6sPlusSpaceHeight) * 3 + 5, iPhone6Plus_iPhone6sPlusWidth, iPhone6Plus_iPhone6sPlusHeight);
        }
        [btn setBackgroundImage:[UIImage imageNamed:@"btnDselect"] forState:(UIControlStateNormal)];
        [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        btn.titleLabel.font = [UIFont systemFontOfSize:24];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.tag = 100+tags++;
        [self.btnArray addObject:btn];
        [self addSubview:btn];
    }
    
    // Button 的点击方法
    for (int i = 0; i < 36; i ++) {
        for (UIButton *button in self.btnArray) {
            if ((button.tag - 100) == i) {
                [button setTitle:self.lowerCase[i] forState:(UIControlStateNormal)];
                [button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
            }
        }
    }
}

// 选择某个按钮
- (void)actionButton:(UIButton *)sender
{
    NSUserDefaults *usr = [NSUserDefaults standardUserDefaults];
    NSString *state = [usr objectForKey:@"buttonState"];
    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btnDselect"]];
    imageV.bounds = CGRectMake(0, 0, CGRectGetWidth(sender.frame), CGRectGetHeight(sender.frame)*1.5);
    if (sender.state == UIControlStateHighlighted) {
        imageV.center = CGPointMake(sender.center.x, sender.center.y - sender.bounds.size.height);
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(sender.frame), CGRectGetHeight(sender.frame)*1.4-10)];
        //        titleLabel.backgroundColor = [UIColor redColor];
        if ([state isEqualToString:@"YES"]) {
            titleLabel.text = self.capitalArray[sender.tag - 100];
        } else {
            titleLabel.text = self.lowerCase[sender.tag - 100];
        }
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:20];
        [imageV addSubview:titleLabel];
        [self addSubview:imageV];
    }
    [imageV performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.2];
    if ([state isEqualToString:@"YES"]) {
        if ([self.textF.text isEqualToString:@""]) {
            self.textF.text = self.capitalArray[sender.tag - 100];
        } else {
            self.textF.text = [self.textF.text stringByAppendingString:self.capitalArray[sender.tag - 100]];
        }
    } else {
        if ([self.textF.text isEqualToString:@""]) {
            self.textF.text = self.lowerCase[sender.tag - 100];
        } else {
            self.textF.text = [self.textF.text stringByAppendingString:self.lowerCase[sender.tag - 100]];
        }
    }
    [usr synchronize];
}

// 切换按钮键
- (void)altBtnAction
{
    NSUserDefaults *usr = [NSUserDefaults standardUserDefaults];
    if (self.altBtn.selected != YES) {
        for (int i = 0; i < 36; i ++) {
            for (UIButton *button in self.btnArray) {
                if ((button.tag - 100) == i) {
                    [button setTitle:self.capitalArray[i] forState:(UIControlStateNormal)];
                    [self.altBtn setBackgroundImage:[UIImage imageNamed:@"alt"] forState:(UIControlStateNormal)];
                }
            }
        }
        [usr setObject:@"YES" forKey:@"buttonState"];
    } else {
        for (int i = 0; i < 36; i ++) {
            for (UIButton *button in self.btnArray) {
                if ((button.tag - 100) == i) {
                    [button setTitle:self.lowerCase[i] forState:(UIControlStateNormal)];
                    [self.altBtn setBackgroundImage:[UIImage imageNamed:@"altBtn"] forState:(UIControlStateNormal)];
                }
            }
        }
        [usr setObject:@"NO" forKey:@"buttonState"];
    }
    self.altBtn.selected = !self.altBtn.selected;
    [usr synchronize];
}

// 删除按钮
- (void)delBtnAction:(UIButton *)sender
{
    if (![self.textF.text isEqualToString:@""]) {
        self.textF.text = [self.textF.text substringToIndex:self.textF.text.length - 1];
    }
}

@end
